layui.use(['laypage', 'form', 'element', 'laytpl','commons'], function() {
	var laytpl = layui.laytpl;
	var element = layui.element;
	var laypage = layui.laypage;
	var form = layui.form;
    var common = layui.commons;


	// 默认参数
	var isHospital = "0"; //不在科
	var page = "1";
	var patientName = ""; //患者姓名
	var admid = ""; //住院号

	//监听submit
	form.on('submit(findhz)', function(data) {
		console.log(data.field);
		console.log(data.field.name);
		patientName = data.field.name;
		admid = data.field.admid;
		getPatientline(isHospital, page,patientName,admid);
		return false;
	});


	// 监听tab
	element.on('tab(demo)', function(data) {
		isHospital = data.index;
		console.log(isHospital);
		getPatientline(isHospital, page,patientName,admid);
	});

	function getPatientline(isHospital, page,patientName,admid) {
        var depId = getUser().depId;
		// 请求参数
		var depId = getUser().depId;
		console.log(depId);
		var param = JSON.stringify({
			"body": {
				"isHospital": isHospital,
				"departmentId": depId,
				"currPage": page,
				"patientName": patientName,
				"admid": admid
			},
			"header": {},
			"token": {}
		});
		$.ajax({
			'type': 'post',
			'url': '/service-patient/patient/viewPatientList',
			'data': param,
			'dataType': 'json',
			'contentType': "application/json",
			'success': function(data) {
				console.log(data);
				var len = data.data.total;
				var pageNum = data.data.pageNum;
				var list = data.data.list;

				laypage.render({
					elem: 'test1',
					count: len, //数据总数，从服务端得到
					curr: pageNum, //起始页
					// groups: 5,
					limit: 10,
					// limits:[8],
					layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
					jump: function(obj, first) {
						//obj包含了当前分页的所有参数，比如：
						console.log("当前页码" + obj.curr); //得到当前页，以便向服务端请求对应页的数据。
						console.log("当前显示" + obj.limit + "条"); //得到每页显示的条数
						//根据当前页请求和渲染模
						// thisData = list.concat().splice(obj.curr*obj.limit - obj.limit, obj.limit);
						// console.log(thisData);	
						page = obj.curr
						var getTpl = demo.innerHTML,
							view = document.getElementById('view');
						laytpl(getTpl).render(list, function(html) {
							view.innerHTML = html;
						});

						//首次不执行
						if (!first) {
							//do something
							getPatientline(isHospital, page,patientName,admid);
						}
					}
				});
			},
			'error': function(res) {
				console.log(res);
			}
		});
	}

	//监听提交
	form.on('submit(formDemo)', function(data) {
		console.log(JSON.stringify(data.field));

		return false;
	});

	getPatientline(isHospital,page,patientName,admid);

})
